$(function () {

    var st;
    var initat = parseInt($(".initsta").val());

    if (initat == 0) {

        var page = parseInt($(".page").val());
        //初始化分页数据
        $.ajax({
            type: "GET",
            url: "dolistmer",
            data: {"form": page},
            dataType: "json",
            success: function (data) {
                data1 = data;
                /*数据不够2条隐藏按钮*/
                if (data1.Page.pageList.length < data1.Page.pageSize || data1.Page.hasNextPage == false) {
                    $(".title").hide();
                } else {
                    $(".page").val(page + 1);//记录页码
                }
                insertDiv(data1);
            }
        });

        //点击加载数据
        $("#more").click(function () {
            $(this).data("status", 1);
            var page = parseInt($(".page").val());
            $(this).html("加载中...");
            status = $(this).data('status');
            if (status == 1) {
                status = $(this).data("status", 0);
                $.ajax({
                    type: "GET",
                    url: "dolistmer",
                    data: {"form": page},
                    dataType: "json",
                    success: function (data) {
                        data1 = data;
                        /*数据不够4条隐藏按钮*/
                        if (data1.Page.pageList.length < data1.Page.pageSize || data1.Page.hasNextPage == false) {
                            $("#more").hide();
                        } else {
                            $(".page").val(page + 1);//记录页码
                        }
                        insertDiv(data1);
                    }
                });
            }

        });

        function insertDiv(data) {
            $(".information1").html("");
            var information = $(".information");
            var html1;
            var datalist = data1.Page.pageList;
            for (var i = 0; i < datalist.length; i++) {
                var datast = datalist[i].merchantsState;
                var state;
                if (datast == 1) {
                    state = '未审批';
                } else if (datast == 2) {
                    state = '已审批';
                }
                html1 = $("<tr><td>" + datalist[i].merchantsShopname + "</td><td>" + datalist[i].merchantsHotlinepho + "</td><td>" + datalist[i].merchantsOpeningdate + "</td><td><span style='color:red'>" + state + "</span></td><td><button type='button' class='btn btn-success btn-sm btn-rounded m-b-2 m-l-5'><a  class='typeinfo' data-tid=" + datalist[i].typeId + " style='color: white;'>查看</a></button><button type='button' class='btn btn-success btn-sm btn-rounded m-b-2 m-l-5'><a  class='typeupdate' data-tid=" + datalist[i].merchantsId + " style='color: white;'>修改</a></button><button type='button' class='btn btn-sm btn-rounded m-b-2 dels' style='background: #d01d13d6;'><a class='typedel' data-tid=" + datalist[i].merchantsId + "  style='color: white;'>删除</a></button></td></tr>");

                //绑定查看点击事件
                html1.find('.typeinfo').bind("click", function () {
                    var id = $(this).data('tid');
                    location.href = "datainfo?tid=" + id;
                });


                //绑定删除点击事件
                html1.find('.typedel').bind("click", function () {
                    var id = $(this).data('tid');
                    swal(
                        {
                            title: "您确定要删除该员工吗？",
                            animation: "slide-from-top",
                            showCancelButton: true,
                            closeOnConfirm: false,
                            cancelButtonText: "取消",
                            confirmButtonText: "确定"

                        },
                        function (inputValue) {
                            if (inputValue === false)
                                return false;
                            $.ajax({
                                url: "dolistmer",
                                type: "GET",
                                data: {
                                    "did": id
                                },
                                datatype: "JSON",
                                success: function (
                                    data) {
                                    if (data) {
                                        swal(
                                            {
                                                title: "操作成功,<span style='color:red'>已删除</span>商家",
                                                html: true,
                                                type: "success",
                                                showCancelButton: false,
                                                showConfirmButton: true,
                                                confirmButtonText: "确定",
                                            },
                                            function () {
                                                location
                                                    .reload();
                                            });
                                    } else {
                                        swal({
                                            title: '删除失败！',
                                            type: 'error',
                                            confirmButtonText: '确定',
                                            closeOnConfirm: false
                                        });
                                    }
                                },
                                error: function (
                                    data) {
                                    swal({
                                        title: '操作失败！',
                                        type: 'error',
                                        confirmButtonText: '确定',
                                        closeOnConfirm: false
                                    });
                                }

                            });
                        });
                });

                //添加数据
                information.append(html1);
            }


            $("#more").html("点击加载更多");
            $("#more").attr("data-status", "1");
        }


    }

    //点击状态筛选数据
    $(".review").click(function () {
        initat = 1;
        if (initat == 1) {
            st = $(this).data("st");
            $(".page").val(1);
            var page = parseInt($(".page").val());

            if (page <= 1) {
                //初始化分页数据
                $.ajax({
                    type: "GET",
                    url: "dolistmer",
                    data: {"form": page, "merchantsState": st},
                    dataType: "json",
                    success: function (data) {
                        data1 = data;
                        if (data1.Page.pageList.length < data1.Page.pageSize || data1.Page.hasNextPage == false) {
                            $(".title").hide();
                        } else {
                            $(".page").val(page + 1);//记录页码
                        }
                        insertDiv(data1);
                    }
                });
            }
            //点击加载数据
            $("#more").click(function () {
                initat = 1;
                $(this).data("status", 1);
                var page = parseInt($(".page").val());
                $(this).html("加载中...");
                status = $(this).data('status');
                if (status == 1) {
                    status = $(this).data("status", 0);
                    $.ajax({
                        type: "GET",
                        url: "dolistmer",
                        data: {"form": page, "merchantsState": st},
                        dataType: "json",
                        success: function (data) {
                            data1 = data;
                            st = data.merchantsState;
                            /*数据不够4条隐藏按钮*/
                            if (data1.Page.pageList.length < data1.Page.pageSize || data1.Page.hasNextPage == false) {
                                $("#more").hide();
                            } else {
                                $(".page").val(page + 1);//记录页码
                            }
                            insertDiv(data1);
                        }
                    });
                }

            });

            function insertDiv(data) {
                $(".information").html("");
                var information1 = $(".information1");
                var html1;
                var datalist = data1.Page.pageList;
                for (var i = 0; i < datalist.length; i++) {
                    var datast = datalist[i].merchantsState;
                    var state;
                    if (datast == 1) {
                        state = '未审批';
                    } else if (datast == 2) {
                        state = '已审批';
                    }
                    html1 = $("<tr><td>" + datalist[i].merchantsShopname + "</td><td>" + datalist[i].merchantsHotlinepho + "</td><td>" + datalist[i].merchantsOpeningdate + "</td><td><span style='color:red'>" + state + "</span></td><td><button type='button' class='btn btn-success btn-sm btn-rounded m-b-2 m-l-5'><a  class='typeinfo' data-tid=" + datalist[i].typeId + " style='color: white;'>查看</a></button><button type='button' class='btn btn-success btn-sm btn-rounded m-b-2 m-l-5'><a  class='typeupdate' data-tid=" + datalist[i].merchantsId + " style='color: white;'>修改</a></button><button type='button' class='btn btn-sm btn-rounded m-b-2 dels' style='background: #d01d13d6;'><a class='typedel' data-tid=" + datalist[i].merchantsId + "  style='color: white;'>删除</a></button></td></tr>");

                    //绑定查看点击事件
                    html1.find('.typeinfo').bind("click", function () {
                        var id = $(this).data('tid');
                        location.href = "datainfo?tid=" + id;
                    });


                    //绑定删除点击事件
                    html1.find('.typedel').bind("click", function () {
                        var id = $(this).data('tid');
                        swal(
                            {
                                title: "您确定要删除该员工吗？",
                                animation: "slide-from-top",
                                showCancelButton: true,
                                closeOnConfirm: false,
                                cancelButtonText: "取消",
                                confirmButtonText: "确定"

                            },
                            function (inputValue) {
                                if (inputValue === false)
                                    return false;
                                $.ajax({
                                    url: "dolistmer",
                                    type: "GET",
                                    data: {
                                        "did": id
                                    },
                                    datatype: "JSON",
                                    success: function (
                                        data) {
                                        if (data) {
                                            swal(
                                                {
                                                    title: "操作成功,<span style='color:red'>已删除</span>商家",
                                                    html: true,
                                                    type: "success",
                                                    showCancelButton: false,
                                                    showConfirmButton: true,
                                                    confirmButtonText: "确定",
                                                },
                                                function () {
                                                    location
                                                        .reload();
                                                });
                                        } else {
                                            swal({
                                                title: '删除失败！',
                                                type: 'error',
                                                confirmButtonText: '确定',
                                                closeOnConfirm: false
                                            });
                                        }
                                    },
                                    error: function (
                                        data) {
                                        swal({
                                            title: '操作失败！',
                                            type: 'error',
                                            confirmButtonText: '确定',
                                            closeOnConfirm: false
                                        });
                                    }

                                });
                            });
                    });

                    //添加数据
                    information1.append(html1);
                }

                $("#more").html("点击加载更多");
                $("#more").attr("data-status", "1");
            }
        }
    });
});
